/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ctrl;

import carte.Carte;
import ctrl.command.Command;
import gl.*;
import java.util.ArrayList;
import perso.Personnage;

/**
 *
 * @author Administrateur
 */
public class WorldCtrl {

    ThreadSimul thread;
    AbstractFactory factory;
    Carte carte;
    int perso_pointeur = -1;
    private ArrayList<Personnage> listPerso = new ArrayList<Personnage>();
    private GUI_Game game;
    

    public void sendParametres(int nbrAgents, int nbrTruands, int weight, int height, int nbPlanques, int speed) {
        thread = new ThreadSimul(this, speed);
        factory = new FactoryTruand();
        carte = ((FactoryTruand) factory).createMap(weight, height, nbPlanques);
        listPerso = ((FactoryTruand) factory).populateWorld(nbrAgents, nbrTruands);
        game.setParam(weight, height);
        game.displayInfo(listPerso, carte);
    }

    public void sendParametres(int nbrPilot, int nbrSith, int nbrJedi, int speed) {
        thread = new ThreadSimul(this, speed);
        factory = new FactoryStarWars();
        carte = ((FactoryStarWars) factory).createMap(10, 10);
        listPerso = ((FactoryStarWars) factory).populateWorld(8, 8);
        game.setParam(10, 10);
        game.displayInfo(listPerso, carte);
    }

    public void doMove() {
        Personnage perso = nextPerso();
        if(perso.endOfGame(listPerso)){
            game.displayEndofGame();
            thread.stopRunning();
            return;
        }
        Command cmd = perso.doAction();
        if(cmd==null){
            int i = 0;
        }
        cmd.execute(listPerso);
        game.displayInfo(listPerso, carte);
        

    }

    public void runSimul() {
        thread.start();
    }

    private Personnage nextPerso() {

        Personnage p = null;
        while (p == null) {
            perso_pointeur++;
            try{
            p = listPerso.get(perso_pointeur);
            }
            catch(Exception e){
               if(perso_pointeur>=listPerso.size()){
                   perso_pointeur=-1;
               }
            }
        }
        return p;

    }

    public void setGame(GUI_Game game) {
        this.game = game;
    }
    
    public ArrayList<Personnage> getPersoList(){
        return listPerso;
    }
}
